
package com.intel.factorytest.ChamberMode;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.app.ActionBar;
import android.view.Window;
import android.os.Message;
import android.os.Looper;
import android.os.Handler;
import android.os.SystemProperties;
import android.app.StatusBarManager;
import android.view.KeyEvent;

import com.intel.factorytest.R;
import com.intel.factorytest.util.LogUtils;
import com.intel.factorytest.util.Util;

import java.io.Serializable;

public abstract class ChambermodeBaseActivity extends Activity {

    private final static String TAG = "ChambermodeBaseActivity";

    private Button mStartButton;
    private Button mStopButton;
    private LinearLayout mButtonView;
    Context mContext;
    private TextView mShowText;
    private TextView mGetMessage;
    ActionBar  actionBar;
    StatusBarManager mStatusBarManager;
    private boolean isShowButton = true;
    protected abstract void startTest();
    protected abstract void stopTest();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.logi( "on create");
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        mContext = getApplicationContext(); 
        //Keep screen on.
        Util.wakeUpAndKeepScreenOn(this);
        Util.setWindowBrightness(this,WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.chamber_mode_test_layout);
        mStatusBarManager = (StatusBarManager)getSystemService(STATUS_BAR_SERVICE);
        initView();
    }

    
    public void HideBottomMenu(){
        LogUtils.logi( "HideBottomMenu");
        mStatusBarManager.disable(StatusBarManager.DISABLE_MASK);
        actionBar = getActionBar();
        if(actionBar != null) {
               actionBar.hide();
        }
        View decorView = this.getWindow().getDecorView();        
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        actionBar = getActionBar();
                        if(actionBar != null) {
                            actionBar.hide();
                        }
                        View decorView = getWindow().getDecorView();
                        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_IMMERSIVE
                                | View.SYSTEM_UI_FLAG_FULLSCREEN;
                        decorView.setSystemUiVisibility(uiOptions);
                    }
            });
    }
    
    public void setWindowBrightness(float brightness) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.screenBrightness = brightness;
        window.setAttributes(lp);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_VOLUME_UP) || (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
           return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    protected void initView() {
        LogUtils.loge("initView");
        mShowText = (TextView) findViewById(R.id.showtext);
        mGetMessage = (TextView) findViewById(R.id.get_message);
        mStartButton = (Button) findViewById(R.id.start);
        mStopButton = (Button) findViewById(R.id.stop);
        if(isShowButton) {
            mButtonView = (LinearLayout)findViewById(R.id.bottom);
            mButtonView.setVisibility(View.GONE);
        }
        mStartButton.setEnabled(false);
        mStopButton.setEnabled(false);

        mStartButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                v.setClickable(false);
                startTest();
            }
        });

        mStopButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                v.setClickable(false);
                stopTest();
                finish();
            }
        });

    }

    @Override
    protected void onResume() {
        LogUtils.logi( "on onResume");
        super.onResume();
        SystemProperties.set("sys.cit.key.test","true");
        HideBottomMenu();
        mStartButton.setEnabled(true);
        mStopButton.setEnabled(true);
        startTest();
    }

    @Override
    protected void onPause() {
        LogUtils.logi( "onPause");
        SystemProperties.set("sys.cit.key.test","false");
        super.onPause();
    }


    @Override
    protected void onDestroy() {
        mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
        Util.setWindowBrightness(this,WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE);
        super.onDestroy();
    }

    @Override
    public void finish() {
        LogUtils.logi( "finish");
        super.finish();
    }

    public void sendDelayedMsg(int msg, long delayMillis, String putString, String value, Handler mHandler){
        LogUtils.logi( "sendDelayedMsg msg = " + msg + ",delaytime = " + delayMillis + ",putString = " + putString + ",value = " + value);
        if(!TextUtils.isEmpty(putString)&&!TextUtils.isEmpty(value)){
            Bundle b = new Bundle();
            b.putString(putString, value);
            Message m = Message.obtain(mHandler, msg);
            m.setData(b);
            mHandler.sendMessageDelayed(m, delayMillis);
        }else{
            Message m = Message.obtain(mHandler, msg);
            mHandler.sendMessageDelayed(m, delayMillis);
        }
    }
    public void updateTitle(String text) {
        LogUtils.logi( "updateTitle text = " + text);
        mShowText.setText(text);
    }

    public void updateMessage(String text) {        
        LogUtils.logi( "updateMessage text = " + text);
        //for debug
        //mGetMessage.setText(text);
    }

}
